package com.shop.shoporder.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.shop.shoporder.common.Encrypt;
import com.shop.shoporder.common.InterfaceVer;
import com.shop.shoporder.common.MessageConstants;
import com.shop.shoporder.common.Response;
import com.shop.shoporder.entity.Classification;
import com.shop.shoporder.service.ClassificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/Classification")
public class ClassificationController {

    @Autowired
    ClassificationService ClassficationService;

    /**
     * 读取商品
     * @param request
     * @param jsonParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/List", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Response List(HttpServletRequest request, @RequestBody JSONObject jsonParam) {

        Map<String, Object> map = InterfaceVer.Security(request, jsonParam);
        if (!map.get("code").toString().equals("000000")) {
            return new Response(MessageConstants.MSG_ACCESS_DENY, null, "接口参数错误", request);
        } else {
            try {
                String res = map.get("parm").toString();
                int AID  = Integer.parseInt(map.get("AID").toString());
                System.out.println("AID:"+AID);
                //将接收到的数据转化为对象
                Classification classification1 = JSON.parseObject(res, new TypeReference<Classification>() {
                });

                //读取数据表
                List<Classification> listClassification = ClassficationService.selectPid(classification1);
                if (listClassification != null) {
                    return new Response(MessageConstants.MSG_SUCCESS, listClassification, listClassification.size(), "", request);
                }
                else
                {
                    return new Response(MessageConstants.MSG_SUCCESS, null, "没有数据", request);
                }
            }
            catch (Exception ex)
            {
                return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, "参数错误", request);
            }
        }
    }

    /**
     * 读取数据
     * @param request
     * @param jsonParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/Self", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Response Self(HttpServletRequest request, @RequestBody JSONObject jsonParam) {

        Map<String, Object> map = InterfaceVer.Security(request, jsonParam);
        if (!map.get("code").toString().equals("000000")) {
            return new Response(MessageConstants.MSG_ACCESS_DENY, null, "接口参数错误", request);
        } else {
            try {
                String res = map.get("parm").toString();
                int AID  = Integer.parseInt(map.get("AID").toString());
                System.out.println("AID:"+AID);
                //将接收到的数据转化为对象
                Classification classification1 = JSON.parseObject(res, new TypeReference<Classification>() {
                });

                Classification classification = ClassficationService.selectByPrimaryKey(classification1.getId());
                if(classification!=null) {
                    return new Response(MessageConstants.MSG_SUCCESS, classification, 1, "", request);
                }
                else
                {
                    return new Response(MessageConstants.MSG_ACCESS_DENY, null, 0, "读取数据错误2", request);
                }
            }
            catch (Exception ex)
            {
                return new Response(MessageConstants.MSG_ACCESS_DENY, null, 0, "读取数据错误1", request);
            }
        }
    }

    /**
     * 添加数据
     * @param request
     * @param jsonParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/Add", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Response AdminAdd(HttpServletRequest request, @RequestBody JSONObject jsonParam) {
        Map<String, Object> map = InterfaceVer.Security(request, jsonParam);
        if (!map.get("code").toString().equals("000000")) {
            return new Response(MessageConstants.MSG_ACCESS_DENY, null, "接口参数错误", request);
        } else {
            try {
                String res = map.get("parm").toString();
                int AID  = Integer.parseInt(map.get("AID").toString());
                System.out.println("AID:"+AID);
                //将接收到的数据转化为对象
                Classification classification = JSON.parseObject(res, new TypeReference<Classification>() {
                });

                int row = ClassficationService.insertSelective(classification);

                if (row > 0) {
                    return new Response(MessageConstants.MSG_SUCCESS, classification, row, "", request);
                } else {
                    return new Response(MessageConstants.MSG_ACCESS_DENY, null, 0, "添加数据错误B", request);
                }
            }
            catch (Exception ex)
            {
                System.out.println(ex.getMessage());
                return new Response(MessageConstants.MSG_ACCESS_DENY, null, 0, "添加数据错误", request);
            }
        }
    }

    /**
     * 编辑数据
     * @param request
     * @param jsonParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/Set", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Response AdminSet(HttpServletRequest request, @RequestBody JSONObject jsonParam) {
        Map<String, Object> map = InterfaceVer.Security(request, jsonParam);
        if (!map.get("code").toString().equals("000000")) {
            return new Response(MessageConstants.MSG_ACCESS_DENY, null, "接口参数错误", request);
        } else {
            try {
                String res = map.get("parm").toString();
                int AID  = Integer.parseInt(map.get("AID").toString());
                System.out.println("AID:"+AID);
                //将接收到的数据转化为对象
                Classification classification = JSON.parseObject(res, new TypeReference<Classification>() {
                });

                System.out.println(classification.getId());
                System.out.println(classification.getName());
                System.out.println(classification.getState());

                int row = ClassficationService.updateByPrimaryKeySelective(classification);
                if (row > 0) {
                    return new Response(MessageConstants.MSG_SUCCESS, classification, row, "", request);
                } else {
                    return new Response(MessageConstants.MSG_ACCESS_DENY, null, 0, "更新数据错误B", request);
                }
            } catch (Exception ex) {
                System.out.println(ex.getMessage());
                return new Response(MessageConstants.MSG_ACCESS_DENY, null, 0, "更新数据错误A", request);
            }
        }
    }



















    @ResponseBody
    @RequestMapping(value = "/Encrypt")
    public String Encrypt(HttpServletRequest request) {
        String key=request.getParameter("key");
        String keyStr = Encrypt.encrypt(key);
        String key1 = Encrypt.decrypt(keyStr);
        return keyStr+"---"+key1;
    }
}